package com.caine.tomcat;

import com.caine.tomcat.config.AppConfig;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import java.io.File;

//@SpringBootApplication
public class TomcatApplication {

    public static void main(String[] args) {
        Tomcat  tomcat = new Tomcat();
        tomcat.setPort(8888);
        File tmpFile = new File(System.getProperty("java.io.tmpdir"));
        System.out.println("tmpFile.getAbsolutePath() = " + tmpFile.getAbsolutePath());
        tomcat.setBaseDir(tmpFile.getAbsolutePath());
        tomcat.addContext("/", tmpFile.getAbsolutePath());
        try {
            tomcat.start();
            AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
            context.register(AppConfig.class);
            context.refresh();
            DispatcherServlet servlet = new DispatcherServlet(context);
            Wrapper app = tomcat.addServlet("/", "app", servlet);
            app.setLoadOnStartup(1);
            app.addMapping("/");
            System.out.println(">>>>>>>>>>tomcat 启动成功");
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

}
